package com.asiainfo.integration.actions.opentext;

import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Usage:
 *
 * @author Charkey
 * @date 2015/12/23
 */
@WebServlet(urlPatterns = "/opentext/oauth", asyncSupported = true)
public class OpenTextOAuthAction  extends HttpServlet {

    public static final Logger INFO_LOG = Logger.getLogger(OpenTextOAuthAction.class);

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String resultType = request.getParameter("result_type");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        response.setHeader("Date", new Date().toString());
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Server", "myserver");
        String faultJson =
                "Authentication Required";
        String failJson1 =
                "{\n" +
                "  \"success\": false,\n" +
                "  \"errors\": [\n" +
                "    {\n" +
                "      \"code\": 400,\n" +
                "      \"reason\": \"invalid_grant\",\n" +
                "      \"details\": \"Resource Owner authentication failed\"\n" +
                "    }\n" +
                "  ]\n" +
                "}\n";
        String failJson2 =
                "{\n" +
                "  \"error\": \"invalid_request\",\n" +
                "  \"error_description\": \"Missing grant_type\"\n" +
                "}\n";
        String successJson =
                "{\n" +
                "    \"access_token\": \"858cb06d-1126-419f-b1a1-39c92d0185a1\",\n" +
                "    \"token_type\": \"Bearer\",\n" +
                "    \"expires_in\": \"3600\",\n" +
                "    \"refresh_token\": \"8b7fe025-c5a5-4f64-bffa-d81eb9355b79\",\n" +
                "    \"scope\": \"agent\",\n" +
                "    \"associate\": {\n" +
                "        \"agentCode\": \"0681000010\",\n" +
                "        \"aadhaarId\": \"\",\n" +
                "        \"name\": \"rakesh\",\n" +
                "        \"Role\": \"\",\n" +
                "        \"areaOffice\": \"\",\n" +
                "        \"functionalManagerId\": \"\",\n" +
                "        \"lineManagerId\": \"\",\n" +
                "        \"parentCompany\": \"\",\n" +
                "        \"Organization\": {\n" +
                "            \"RJILDepartment\": \"\",\n" +
                "            \"Org\": \"\",\n" +
                "            \"name\": \"\",\n" +
                "            \"distributionCenter\": \"\",\n" +
                "            \"city\": \"\",\n" +
                "            \"state\": \"\",\n" +
                "            \"circle\": \"\"\n" +
                "        },\n" +
                "        \"ContactDetails\": {\n" +
                "            \"email\": \"\",\n" +
                "            \"phoneNumber\": \"\"\n" +
                "        },\n" +
                "        \"memberOfGroups\": [],\n" +
                "        \"group\": \"\"\n" +
                "    }\n" +
                "}";
        if ("success".equals(resultType)) {
            response.getWriter().print(successJson);
        } else if ("fail1".equals(resultType)) {
            response.getWriter().print(failJson1);
        } else if ("fail2".equals(resultType)) {
            response.getWriter().print(failJson2);
        } else if ("fault".equals(resultType)) {
            response.getWriter().print(faultJson);
        } else {
            response.getWriter().print(successJson);
        }
    }
}
